#!/usr/bin/fish

set name $argv[1]
set skip false

set SysDir $HOME/Repo
set BldDir $HOME/Build
set RcsDir $BldDir/rcs
set TmpDir $BldDir/tmp
set SrcDir $TmpDir/src
set PkgDir $TmpDir/pkg

set RcsUrl http://sysrcs.googlecode.com/git
set RecipeExt .recipe
set SystemExt .system
set PkgFileExt pkg.txz
set GlobalSQLite global.sqlite

function MakeEnvDirs
	set path $RcsDir $SrcDir/$name $PkgDir/$name

	for i in $path
		if not test -d $i
			mkdir -p $i
		end
	end
end

function FetchScript
	set RcsFile $RcsDir/$name/$name$RecipeExt
	set RcsUrlDir $RcsUrl/$name

	if not test -f $RcsFile
		cd $RcsDir
		echo info: fetching recipe: $name

		wget -nv -r -l1 -np -nH --cut-dirs=1 --reject=html $RcsUrlDir

		echo "      done"
		cd $BldDir
	end

end

function GetSrcFile
	. $RcsDir/$name/$name$RecipeExt

	set srcfile (basename $url)

	if not test -f $TmpDir/$srcfile
		cd $TmpDir

		echo "      downloading: $srcfile"
		wget -nv $url

		cd $BldDir
	end
end

function ExtractFile
	. $RcsDir/$name/$name$RecipeExt

	set srcfile (basename $url)
	echo "      extracting: $srcfile"

	cd $TmpDir
	tar -C $SrcDir/$name -xf $srcfile
	cd $BldDir
end

function CompileSrc
	. $RcsDir/$name/$name$RecipeExt

	if not test $pth
		set pth $pkg-$ver
	end

	cd $srcdir/$pth

	srcSetup

	cd $BldDir
end

function CompilePkg
	. $RcsDir/$name/$name$RecipeExt

	if not test $pth
		set pth $pkg-$ver
	end

	cd $srcdir/$pth

	pkgSetup

	cd $BldDir
end

function CreateDBase
	if not test -f $SysDir/$GlobalSQLite
		sqlite3 $SysDir/$GlobalSQLite "create table pkgs (pkg text primary key, ver text, grp text, lst text, sys text);"
	end
end

function CompressPkg
	. $RcsDir/$name/$name$RecipeExt

	echo "      creating filelist"
	cd $pkgdir

	touch ../filelist.txt
	for i in (find -type f | sort)
		echo $i >> ../filelist.txt
	end
	sed -i -e 's/.\//\//' ../filelist.txt
	set lst (cat ../filelist.txt)

	rm ../filelist.txt

	echo "      compressing file"
	if not test -d $SysDir/$grp
		mkdir -p $SysDir/$grp
	end

	set pkgfile $pkg-$ver-$PkgFileExt
	if test -f $SysDir/$grp/$pkgfile
		rm -f $SysDir/$grp/$pkg-[0-9]*
	end

	rm -f $pkgdir$shrdir/info/dir

	fakeroot tar -cJf $SysDir/$grp/$pkgfile ./

	set sys ""
	set SysFile $RcsDir/$name/$name$SystemExt
	if test -f $SysFile
		set sys (cat $SysFile)
	end

	CreateDBase
	sqlite3 $SysDir/$GlobalSQLite "delete from pkgs where pkg = '$pkg';"
	sqlite3 $SysDir/$GlobalSQLite "insert into pkgs(pkg, ver, grp, lst, sys) values('$pkg', '$ver', '$grp', '$lst', '$sys');"

	cd $BldDir
end

function CleanEnvDir
	rm -rf $PkgDir/$name
	rm -rf $SrcDir/$name
end

function MakePackage
	MakeEnvDirs
	FetchScript

	. $RcsDir/$name/$name$RecipeExt

	echo info: preparing source: $pkg-$ver
	GetSrcFile
	ExtractFile
	echo "      done"

	if [ $skip = true ]
		echo info: creating package: $pkg-$ver
		CompressPkg
		CleanEnvDir
		echo "      done"		
	else

		echo info: compiling source: $pkg-$ver
		CompileSrc
		CompilePkg
		echo "      done"

		echo info: creating package: $pkg-$ver
		CompressPkg
		CleanEnvDir
		echo "      done"
	end
end

if not test $name
	echo warn: missing flag
	exit 0
end

for i in $argv
	if [ $i = "--skip-src" ]
		set skip true
	end
end

set bindir /usr/bin
set etcdir /etc
set libdir /usr/lib
set rundir /run
set shrdir /usr/share
set usrdir /usr
set vardir /var

set pkgdir $PkgDir/$name
set srcdir $SrcDir/$name

MakePackage

